1 /*
2 * Angkor Web Framework
3 *
4 * Distributable under LGPL license.
5 * See terms of license at gnu.org.
6 */
7
8 package com.tirsen.angkor;
9
10 import com.tirsen.angkor.Application;
11 import com.tirsen.angkor.Debug;
12 import com.tirsen.angkor.RenderContext;
13 import com.tirsen.angkor.View;
14 import com.tirsen.angkor.widget.Container;
15 import org.apache.log4j.Category;
16
17 import java.io.IOException;
18 import java.io.Serializable;
19 import java.util.HashMap;
20 import java.util.Iterator;
21 import java.util.Map;
22
23 /***
24 * The default implementation of the View interface, implements a set of functionality
25 * usable by most widgets in Angkor.
26 *
27 * These needs to be serializable to support being serialized in the session while
28 * being referenced as parsing components.
29 *
30 * <!-- $Id: Component.java,v 1.4 2002/10/13 19:59:22 tirsen Exp $ -->
31 *
32 * @author $Author: tirsen $
33 * @version $Revision: 1.4 $
34 */
35 public abstract class Component implements View, Serializable
36 {
37 private static final Category logger = Category.getInstance(Debug.LOGGER_NAME);
38
39 private View container;
40 private String id;
41 private boolean visible = true;
42 private Map attributes;
43
44 private boolean debugTables = false;
45
46 /***
47 * Render with specified ID. The ID is checked for uniqueness and an error
48 * will occur at render-time if it is not unique.
49 * This is id is useful when doing functional testing.
50 *
51 */
52 public Component(String id)
53 {
54 this.id = id;
55 }
56
57 /***
58 * Create with generated ID.
59 */
60 public Component()
61 {
62 this.id = null;
63 }
64
65 public View getContainer()
66 {
67 return container;
68 }
69
70 public void setContainer(View container)
71 {
72 if (container != null && this.container != null && this.container != container)
73 {
74 throw new IllegalStateException("Can not add an element to more than one container.");
75 }
76 this.container = container;
77 }
78
79 public abstract void render(RenderContext context) throws IOException;
80
81 /***
82 * Set an attribute which can be used by for example event-listeners to acquire more information
83 * about the event.
84 * For example a button or a link in a table could have an attribute for which object-id of the
85 * row that was clicked.
86 */
87 public void setAttribute(String name, Object value)
88 {
89 if (attributes == null) attributes = new HashMap();
90 attributes.put(name, value);
91 }
92
93 public Object getAttribute(String name)
94 {
95 if (attributes == null) return null;
96 return attributes.get(name);
97 }
98
99 /***
100 * @deprecated use {@link #getId()} instead.
101 * @param context
102 * @return
103 */
104 public String uniqueId(RenderContext context)
105 {
106 return getId();
107 }
108
109 /***
110 * Sets a custom id to be used instead of a generated one.
111 * The custom id is checked for uniqueness at each use.
112 * Warning! This should really only be used when initializing the component, if the component
113 * has been registered for parsing it will
114 */
115 public void setId(String id)
116 {
117 this.id = id;
118 }
119
120 /***
121 * Forces this component to allocate a unique id.
122 */
123 public void allocateUniqueId(RenderContext context)
124 {
125 uniqueId(context);
126 }
127
128 public String getId()
129 {
130 if (id == null)
131 {
132 id = Application.getApplication().nextUniqueID();
133 // checks and allocates id.
134 Application.getApplication().allocateUniqueID(id, this);
135 }
136 return id;
137 }
138
139 public void setVisible(boolean visible)
140 {
141 this.visible = visible;
142 }
143
144 public boolean isVisible()
145 {
146 return visible;
147 }
148
149 public boolean isParsing()
150 {
151 return false;
152 }
153
154 public void parse(RenderContext context)
155 {
156 debugTables = context.getRequestParameter("debugTables") != null;
157 }
158
159 /***
160 * Returns which index this has of all visible elements of this container.
161 */
162 public int getVisibleIndex()
163 {
164 Iterator it = ((Container) getContainer()).iterateAllElements();
165 int index = -1;
166 while (it.hasNext())
167 {
168 View view = (View) it.next();
169 if (view.isVisible()) index++;
170 if (view == this) break;
171 }
172 return index;
173 }
174
175 public boolean isDebugTables()
176 {
177 return debugTables;
178 }
179
180 protected RenderContext getRenderContext()
181 {
182 return RenderContext.getRenderContext();
183 }
184 }
This page was automatically generated by Maven